package domainlogic.ai;

import java.util.List;

import other.Life;
import other.Modifier;
import other.MoodModifiers;
import other.Targetable;
import other.World;

public class MovementAi {

	public static void update(Life life){
		
		List<Modifier> modifiers = life.getModifiers();

		for (int i = 0; i < modifiers.size(); i++) {
			if(modifiers.get(i) == MoodModifiers.Random){
				if(Math.random() < 0.05) {
					life.setxSpeed((float)(((Math.random()*2)-1)*life.getMaxXSpeed()));
					life.setySpeed((float)(((Math.random()*2)-1)*life.getMaxYSpeed()));

				}if(Math.random() < 0.05){
//					walkTowards(life, life.getTarget().getCenterX(), life.getTarget().getCenterY());
					if(life != World.getLives().get(0)){
						walkTowards(life, World.getLives().get(0));
					}
				}						
			}
		}
		if(life.getTarget() != null){
			if(Math.random()>0.9){
				walkTowards(life, life.getTarget().getCenterX(), life.getTarget().getCenterY());

			}
		}
		life.move(life.getxSpeed(), life.getySpeed());
	}
	
	public static void walkTowards(Life life, Targetable life2){
		walkTowards(life, life2.getCenterX(), life2.getCenterY());
	}
	
	public static void walkTowards(Life life, float x, float y){
		//HERP DERP FIXA FRID�N
		float dx = x - life.getX();
		float dy = y - life.getY();
		double ratio = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy,2));
		dx = (float) (dx / ratio);
		dy = (float) (dy / ratio);
		life.setxSpeed(dx * life.getMaxXSpeed());
		life.setySpeed(dy * life.getMaxYSpeed());
	}
	
}
